//
//  DSSHRegistBaseV.m
//  DSSH－M
//
//  Created by Fuhao on 16/3/29.
//  Copyright © 2016年 Fuhao. All rights reserved.
//

#import "DSSHRegistBaseV.h"
#import "RegistModel.h"
static  int _sec;
@interface DSSHRegistBaseV ()

{
    NSString *_YanzhenMa;
    UITextField *_sectext;
    NSTimer *_timer;
    NSString *YanZ;
    RegistModel * model;
    
    
}


@end

@implementation DSSHRegistBaseV
-(id)initwithstoryboard:(NSString *)storyboard
{
    UIStoryboard *storyboardV = [UIStoryboard storyboardWithName:storyboard bundle:nil];
    return [storyboardV instantiateInitialViewController];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    if(!_typeofview)
    {
        [self.navigationItem setTitle:@"注册"];
        _button.titleLabel.text = @"注册 ";
        [_button setTitle:@"注册 " forState:UIControlStateNormal];
        _button.layer.cornerRadius =3;
    }
    else{
        [self.navigationItem setTitle:@"找回密码"];
        [_button setTitle:@"提交 " forState:UIControlStateNormal];
        _button.layer.cornerRadius =3;
        
    }
    self.view .backgroundColor = BACKGROUD_COLOR;
    
    _yanzheng.layer.borderWidth = 0.5;
    _yanzheng.layer.borderColor = [UIColor redColor].CGColor;
    _yanzheng.layer.cornerRadius =5;
    
    UIView *borde = [[UIView alloc] init];
    [_yanzheng.superview addSubview:borde];
    borde.backgroundColor = BACKGROUD_COLOR;
    [borde mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@1);
        make.height.equalTo(@35);
        make.right.mas_equalTo(_yanzheng.mas_left).with.offset(-15);
        make.centerY.mas_equalTo(_yanzheng.superview.mas_centerY);
        
    }];
    [_littlebutton setHidden:_typeofview];
    [_text setHidden:_typeofview];
    [_redtext setHidden:_typeofview];
    
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)GetYanzhen:(id)sender {
     AccountService * _service = [AccountService getObject];
    //启动服务
    if(_phoneN.text.length == 0)
    {
        [self.view makeToast:@"手机号不能为空"];
        return;
    }
    if(_phoneN.text.length != 0 && _phoneN.text.length < 11)
    {
        [self.view makeToast:@"手机号不足11位"];
        return;
    }
    [_service getCheckCode:_phoneN.text];
    _service.httpRequestResultBlock = ^(id result , int coder)
    {
        if(coder == NET_ERROR)
        {
            [self.view makeToast:@"网络连接异常"];
        }
    };
    _service.saveToGlobalData = ^(id data,int coder)
    {
         _reModel = data;
        
        
        if(coder == 1)
        {
            if(_reModel.retCode !=0)
            {
                [self.view makeToast:[NSString stringWithFormat:@"%@",self. reModel.message]];
                return ;
            }
            _YanzhenMa = self.reModel.result;
            [self.view makeToast:[NSString stringWithFormat:@"%@",self. reModel.message]];
            //返回成功开始计时,获取默认的全局队列并且将button覆盖
            _sectext = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, _yanzheng.bounds.size.width, _yanzheng.bounds.size.height)];
            _sectext.tintColor = [UIColor grayColor];
           _sectext.backgroundColor = [UIColor whiteColor];
            [_sectext setTextAlignment:NSTextAlignmentCenter];
            _sectext.font = [UIFont systemFontOfSize:13.0];
            _yanzheng.layer.borderColor = [UIColor grayColor].CGColor;
            [_yanzheng addSubview:_sectext];
            _yanzheng.enabled =NO;
            _sectext.userInteractionEnabled = NO;
            _sec = 60;
            _sectext.text = [NSString stringWithFormat:@"%ds后获取",_sec];
            dispatch_queue_t  queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            dispatch_async(queue, ^{
                //创建timer并添加到当前线程的runloop中
                _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
                [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
                [[NSRunLoop currentRunLoop] run];
                [_timer fire];
        });
    }
    
};

    
    

}
-(void)updateTimer:(NSTimer *)timer
{
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新按钮上的字体
        _sec -- ;
        _sectext.text = [NSString stringWithFormat:@"%ds后获取",_sec];
        if(_sec == 0)
        {
            

            [_timer invalidate];
            [_sectext removeFromSuperview];
            _sectext = nil;
            _yanzheng.enabled =YES;
            _sec = 60;
        }
    });
}
- (IBAction)regist:(id)sender {
    if(_passW.text.length ==0)
    {
        [self.view makeToast:@"密码不能为空!"];
    }
    if(![_Yanzhen.text  isEqualToString: _YanzhenMa])
    {
        [self.view makeToast:@"验证码输入错误"];
        return;
    }
    AccountService * _service = [AccountService getObject];

    [_service regMember:_phoneN.text passw:_passW.text];
    _service.httpRequestResultBlock = ^(id result , int coder)
    {
        if(coder == NET_ERROR)
        {
            [self.view makeToast:@"网络连接异常"];
        }
    };
    _service.saveToGlobalData = ^(id data ,int code)
        {
            model = data;
            if([model.retcode intValue] !=1)
            {
                [self.view makeToast:[NSString stringWithFormat:@"%@",self.reModel.message]];
                return ;
            }
            [_timer invalidate];
            //注册成功后将用户信息储存到偏好
            
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            [user setObject:_passW.text forKey:@"userPassWord"];
            [user setObject:_phoneN.text forKey:@"userphone"];
            //返回登录界面
            [self.navigationController popViewControllerAnimated:YES];

        };
    [_service regMember:_phoneN.text passw:_passW.text];
//    _service.saveToGlobalData = ^(id data ,int coder)
//    {
//        _reModel = data;
//        if(coder ==1)
//        {
//                    }
//    };

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
